﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports; //串口调试用
using System.Text.RegularExpressions;

namespace FHtool
{
    public partial class SerialToolForm : Form
    {
        public static bool formIsOpen = false;

        private Serial serial = new Serial();

        public SerialToolForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void SerialToolForm_Load(object sender, EventArgs e)
        {
            formIsOpen = true;

            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            tbxRcvData.Multiline = true;  //指定文本框是否为多行文本框
            tbxRcvData.MaxLength = 32767; //在文本框中最多输入的文本的字符个数
            tbxRcvData.WordWrap = true;   //文本框中的文本自动换行
            tbxRcvData.ReadOnly = true;   //只读文本框
            tbxRcvData.ScrollBars = ScrollBars.Vertical; //垂直滚动条
            //tbxRcvData.Width = this.Width;
            //tbxRcvData.Height = (int)(this.Height * 0.8);
            //tbxRcvData.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            tbxRcvData.BackColor = Color.Black;
            tbxRcvData.ForeColor = Color.Green;

            tbxSendData.Multiline = true;  //指定文本框是否为多行文本框
            tbxSendData.MaxLength = 32767; //在文本框中最多输入的文本的字符个数
            tbxSendData.WordWrap = true;   //文本框中的文本自动换行
            tbxSendData.ScrollBars = ScrollBars.Vertical; //垂直滚动条
            //tbxSendData.Width = this.Width;
            //tbxSendData.Height = (int)(this.Height * 0.2);
            //tbxSendData.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            //tbxSendData.BackColor = Color.Black;
            //tbxSendData.ForeColor = Color.Green;

            btnSerialOpen.Text = Serial.serialPort.IsOpen ? "关闭串口" : "打开串口";
            pictureBox1.BackgroundImage = Serial.serialPort.IsOpen ? Properties.Resources.蓝点 : Properties.Resources.红点;


            Serial.SpRecvHandler += new Serial.SpRecvEventHandler(Serial_SpRecvHandler); //串口接收事件回调注册
        }

        #region 串口接收处理

        private void Serial_SpRecvHandler(params byte[] buffer)
        {
            if (checkBoxHexRecv.Checked) //16进制显示
            {
                foreach (byte b in buffer)
                {
                    tbxRcvData.AppendText(b.ToString("X2") + " ");
                }
            }
            else
            {
                tbxRcvData.AppendText(Encoding.Default.GetString(buffer));
            }
            tssLabel_recvcount.Text = "R：" + Serial.received_count.ToString();
        }
        #endregion

        private void SerialToolForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            formIsOpen = false;
        }

        #region 发送按钮
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (checkBoxHexSend.Checked) //16进制发送
            {
                MatchCollection mc = Regex.Matches(tbxSendData.Text, @"(?i)[\da-f]{2}");
                List<byte> buf = new List<byte>();//填充到这个临时列表中，比用数组好很多
                foreach (Match m in mc) //依次添加到列表中
                {
                    buf.Add(byte.Parse(m.Value)); //将m.Value转换为byte添加到列表buf中
                }
                Serial.Write(buf.ToArray());
            }
            else
            {
                Serial.Write(tbxSendData.Text);
            }
        }
        #endregion

        #region 串口详细配置
        private void btnSerialConfig_Click(object sender, EventArgs e)
        {
            if (!SerialConfigForm.formIsOpen)
            {
                SerialConfigForm form = new SerialConfigForm(Serial.serialPort);
                //SerialConfigForm form = new SerialConfigForm();
                form.TopMost = true;
                form.WindowState = FormWindowState.Normal;
                form.ShowDialog();
            }
        }
        #endregion

        #region 串口打开
        private void btnSerialOpen_Click(object sender, EventArgs e)
        {
            if (Serial.serialPort.IsOpen == false)
            {
                Serial.OpenPort();
            }
            else
            {
                Serial.ClosePort();
            }
            btnSerialOpen.Text = Serial.serialPort.IsOpen ? "关闭串口" : "打开串口";
            pictureBox1.BackgroundImage = Serial.serialPort.IsOpen ? Properties.Resources.蓝点 : Properties.Resources.红点;
        }
        #endregion

        #region 清接收区
        private void btnClearRecvText_Click(object sender, EventArgs e)
        {
            tbxRcvData.Text = "";
        }

        #endregion

        #region 清发送区
        private void btnClearSendText_Click(object sender, EventArgs e)
        {
            tbxSendData.Text = "";
        }
        #endregion
    }
}
